package cn.fxl.basic.controller;

import cn.fxl.basic.dto.SmsCodeDto;
import cn.fxl.basic.exception.BusinessExciption;
import cn.fxl.basic.result.AjaxResult;
import cn.fxl.basic.service.IVerifyCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 图形验证码
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService iVerifyCodeService;
    @GetMapping("/image/{key}")
    public AjaxResult getPhoneCode(@PathVariable("key") String key){
        try {
            String str=iVerifyCodeService.getPhoneCode(key);
            return AjaxResult.me().setResultObj(str);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取验证码失败");
        }
    }
    @PostMapping("/smsCode")
    public AjaxResult sendSmsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            iVerifyCodeService.sendSmsCode(smsCodeDto);
            return new AjaxResult();
        } catch (BusinessExciption businessExciption) {
           return new AjaxResult(false,businessExciption.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙");
        }
    }

    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody Map<String,String> map){
        try {
            iVerifyCodeService.binderSmsCode(map);
            return new AjaxResult();
        } catch (BusinessExciption businessExciption) {
            return new AjaxResult(false,businessExciption.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙");
        }
    }
}
